import Sprite from '../base/sprite'
import DataBus  from '../databus'
import _COIGIG from './mapdata'
import Beans   from './beans'

const screenWidth  = window.innerWidth
const screenHeight = window.innerHeight

const BG_IMG_SRC   = 'images/bg.jpg'

 var counttimer ;            //数字计时
 var labeltimes=3;           //3,2,1
 
 const ENTRY_TEXT=['第一关','第二关','第三关','第四关','第五关','第六关','第七关','第八关','第九关','第十关','第十一关','第十二关'];

 var _COLOR = ['#F00','#F93','#0CF','#F9C'];//NPC颜色
 var _LIFE = 5;				//玩家生命值
 var _SCORE = 0;				//玩家得分
	
 	//var Databus.gamestart=false        //全局游戏开始标志

 const _COS = [1,0,-1,0]
 const _SIN = [0,1,0,-1]

	
let databus2 = new DataBus()

/**
 * 游戏背景类
 * 提供update和render函数实现无限滚动的背景功能
 */
export default class BackGround extends Sprite {
	
	
  constructor(ctx) {
    super("", 0, 0) //不需背景图片

    this.top = 0

	labeltimes=3 //重新初始化
	//databus2.GAME_INDEX=0
	var index=databus2.GAME_INDEX
	
	
	this.x_length=_COIGIG[index]['map'][0].length
	this.y_length=_COIGIG[index]['map'].length
    console.log(this.x_length)
	console.log(this.y_length)
	
	
	console.log(_COIGIG[index]['wall_color'])
	
	//databus2.sizex=20 //地图单元的宽度
	//databus2.size=20 //地图单元的宽度
	
	this.times=1                 //for map items
	
	var myDate = new Date();
	this.curmill= myDate.getTime(); //毫秒数
	
	databus2.gameOver=false
    this.render(ctx)
	
	//显示3,2,1倒计时
    counttimer=setInterval(
	function (){
	   console.log(labeltimes)
	   if (labeltimes>0) //for 内函数里面必须使用全局变量(labeltimes)
         labeltimes--;
       else{
	     clearInterval(counttimer);
		 databus2.gamestart=true;
		 //databus2.breakflag=false;
	   }
 
	},
	1000);
  }
    
	setbeans(beans){
		this.beans=beans
	}
  
    
    //画地图
	drawmap(context,mapindex){
		
        //console.log("draw map begin")
		
		context.lineWidth = 2;
		
		for(var j=0; j<this.y_length; j++){
			for(var i=0; i<this.x_length; i++){
				var value = this.get(i,j);
				if(value){
					var code = [0,0,0,0];
					if(this.get(i+1,j)&&!(this.get(i+1,j-1)&&this.get(i+1,j+1)&&this.get(i,j-1)&&this.get(i,j+1))){
						code[0]=1;
					}
					if(this.get(i,j+1)&&!(this.get(i-1,j+1)&&this.get(i+1,j+1)&&this.get(i-1,j)&&this.get(i+1,j))){
						code[1]=1;
					}
					if(this.get(i-1,j)&&!(this.get(i-1,j-1)&&this.get(i-1,j+1)&&this.get(i,j-1)&&this.get(i,j+1))){
						code[2]=1;
					}
					if(this.get(i,j-1)&&!(this.get(i-1,j-1)&&this.get(i+1,j-1)&&this.get(i-1,j)&&this.get(i+1,j))){
						code[3]=1;
					}
					
					
					if(code.indexOf(1)>-1){
						if (value==2)
						  context.strokeStyle="#FFF"
					    else
						  context.strokeStyle=_COIGIG[mapindex]['wall_color']
					  // 
					  
						var pos = this.coord2position(i,j);
						
						switch(code.join('')){
							case '1100':
								context.beginPath();
								context.arc(pos.x+databus2.size/2,pos.y+databus2.size/2,databus2.size/2,Math.PI,1.5*Math.PI,false);
								context.stroke();
								context.closePath();
								break;
							case '0110':
								context.beginPath();
								context.arc(pos.x-databus2.size/2,pos.y+databus2.size/2,databus2.size/2,1.5*Math.PI,2*Math.PI,false);
								context.stroke();
								context.closePath();
								break;
							case '0011':
								context.beginPath();
								context.arc(pos.x-databus2.size/2,pos.y-databus2.size/2,databus2.size/2,0,.5*Math.PI,false);
								context.stroke();
								context.closePath();
								break;
							case '1001':
								context.beginPath();
								context.arc(pos.x+databus2.size/2,pos.y-databus2.size/2,databus2.size/2,.5*Math.PI,1*Math.PI,false);
								context.stroke();
								context.closePath();
								break;
							default:
								
								//if (i==14 && j==22)
								//	continue;
								//if (i==14 && j==24)
								//	continue;
								
								var dist = databus2.size/2;
								code.forEach(function(v,index){
									//console.log("v="+v)
									if(v){
										context.beginPath();
										context.moveTo(pos.x,pos.y);
										context.lineTo(pos.x-_COS[index]*dist,pos.y-_SIN[index]*dist);
										context.stroke();
										context.closePath();							
									}
								});
						}
					}
				}
				/*
				if (i>=0 && i<=20 && j==23){
				var pos = this.coord2position(i,j);
                var dist = databus2.size/2;
				context.beginPath();
				context.moveTo(pos.x,pos.y);
				context.lineTo(pos.x+2*dist,pos.y);
				context.moveTo(pos.x,pos.y);
				context.lineTo(pos.x,pos.y+2*dist);
				context.moveTo(pos.x+2*dist,pos.y);
				context.lineTo(pos.x+2*dist,pos.y+2*dist);
				context.moveTo(pos.x,pos.y+2*dist);
				context.lineTo(pos.x+2*dist,pos.y+2*dist);
				context.stroke();
				context.closePath();											 						
				}*/
			}
		}
    }
	
	//画物品
	drawitems (context,mapindex){
		//console.log("draw items begin")
		
		//console.log(_COIGIG[mapindex]['goods']['1,3'])
		
		for(var j=0; j<this.y_length; j++){
			for(var i=0; i<this.x_length; i++){
				
				//if (j==23) //吃豆
				//   continue;
				
				if(!this.get(i,j)){
					var pos = this.coord2position(i,j);
					context.fillStyle = "#F5F5DC";
					if(_COIGIG[mapindex]['goods'][i+','+j]){
						context.beginPath();
						//times=10
						context.arc(pos.x,pos.y,3+this.times%2,0,2*Math.PI,true);
						context.fill();
						context.closePath();
					}else{
						context.fillRect(pos.x-2,pos.y-2,4,4);
					}
				}else{
					//if (j==23)
					 //  console.log("item"+"i="+i+",j="+j)
				}
			}
		}
	}
  
  
    update() {
	  var myDate = new Date();
	  var nowmill= myDate.getTime(); //毫秒数
	
	  if (nowmill-this.curmill>=500){ //half second
	    this.times++;
	    this.curmill=nowmill;
	  }
	  
	  if (typeof this.beans=='undefined')
		  return 
	  //豆被吃完了，某关结束了
	  if (this.beans.isempty(databus2.GAME_INDEX)){
	  //if (this.beans.ebnum==10){
            databus2.gamestart=false
		    databus2.gameOver=true
			databus2.GAME_INDEX++ //关数+1
			databus2.mapdataflag=false
			
			if (databus2.GAME_INDEX==this.GAME_LASTINDEX)
				databus2.gameALLOver=true
	  }
    }
	

  /**
   * 背景图画地图函数
   */
  render(ctx) {
    
	var index=databus2.GAME_INDEX
	
	
	//显示3,2,1
	this.shownumlabel(ctx)
	   
    //if (databus2.gamestart==true){
	this.drawmap(ctx,index)
	 //  this.drawdbclickinfo(ctx)
	this.drawscore(ctx) 
	//}
	
  }
  
  shownumlabel(ctx){
    if (labeltimes>=1){ 
			ctx.fillStyle = "#ffffff"
			ctx.font    = "20px Arial"
            var numstr=labeltimes
			var mappos=this.coord2position(12,14)
			ctx.fillText(
			  numstr,
			  mappos.x+15,
			  mappos.y+30
			)
	}
  }
  
  drawdbclickinfo(context){
     var dbclickinfo=databus2.getdoubleckick()
	 context.fillStyle = "#ffffff"
	 context.font    = "13px Arial"
	 context.fillText(
			  "lasttime:"+dbclickinfo.lt,
			  screenWidth  - 180,
			  screenHeight / 2 
			)
			
	 context.fillText(
			  "curtime:"+dbclickinfo.it,
			  screenWidth  - 180,
			  screenHeight / 2+50 
			)
			
     context.fillText(
			  "offset:"+dbclickinfo.ot,
			  screenWidth  - 180,
			  screenHeight / 2+100 
			)
	 context.fillText(
			  "dbtimes:"+dbclickinfo.mvnum,
			  screenWidth  - 180,
			  screenHeight / 2+150 
			)
			
	 var state="run"
	 if (databus2.gamepause) {//pause
	    state="pause"
	 }
	 
	 context.fillText(
			  state,
			  screenWidth  - 180,
			  screenHeight / 2+250 
			)
	
	

  }
  
  drawscore(context){ //画关卡和分数
    var height=170
    /*
	context.font = 'bold 20px Helvetica';
	context.textAlign = 'left';
	context.textBaseline = 'bottom';
	context.fillStyle = '#C33';
	context.fillText('SCORE',screenWidth  - 180,height);
	
	context.font = '20px Helvetica';
	context.textAlign = 'left';
	context.textBaseline = 'top';
	context.fillStyle = '#FFF';
	context.fillText(databus2.score,screenWidth  - 180+12,height+20);
	*/
	
	var right=databus2.size*this.x_length+databus2.MAP_OFFSETX
	var offset=30
	context.font = 'bold 20px Helvetica';
	context.textAlign = 'left';
	context.textBaseline = 'bottom';
	context.fillStyle = '#C33';
	
	var index=databus2.GAME_INDEX+1
	context.fillText('LEVEL: '+index,right+offset,height);
	/*
	context.font = 'bold 20px Helvetica';
	context.textAlign = 'left';
	context.textBaseline = 'top';
	context.fillStyle = '#C33';
	context.fillText(databus2.GAME_INDEX+1,right+offset+80,height-20);
	*/
  }
	  
}
